package com.hbt.orderfood.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.utils.StringUtils;

import java.util.LinkedList;
import java.util.List;

public class UserFoodAdapter extends RecyclerView.Adapter {
    public void setFoods(List<Food> foods) {
        this.foods = foods;
    }

    private List<Food> foods = new LinkedList<>();
    public UserFoodAdapter(List<Food> foods ){
        this.foods = foods;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layout = new LinearLayout(parent.getContext());
        layout.setLayoutParams(params);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_food_item,layout,true);
        ViewHolder holder = new ViewHolder(view);
        //        System.out.println("onCreateViewHolder");
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder hold = (ViewHolder)holder;
        Food food = foods.get(position);
        hold.tv_name.setText(food.getName());
        hold.tv_price.setText("单价："+String.valueOf(food.getPrice())+"￥");
        StringUtils.setImg(hold.img,food.getType());
        hold.add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for (Order o:StringUtils.orders){
                    if (o.getFood_id()==food.getId()){
                        o.setNum(o.getNum()+1);
                        o.setPrice(o.getPrice()+food.getPrice());
                        listener.clickAddBtn();
                        return;
                    }
                }
                Order order = new Order();
                order.setFood_id(food.getId());
                order.setUser_id(StringUtils.USERID);
                order.setPrice(food.getPrice());
                order.setNum(1);
                StringUtils.orders.add(order);
                listener.clickAddBtn();
            }
        });
    }//onBindViewHolder

    @Override
    public int getItemCount() {
        return foods.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_name;
        TextView tv_price;
        Button add_btn;
        ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.name);
            tv_price = itemView.findViewById(R.id.price);
            add_btn = itemView.findViewById(R.id.add_btn);
            img = itemView.findViewById(R.id.img);
        }
    }

    public interface OnClickAddBtnListener{
        void clickAddBtn();
    }

    public void setListener(OnClickAddBtnListener listener) {
        this.listener = listener;
    }

    private OnClickAddBtnListener listener;

}//class
